const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

// 3. 设置 node.js 环境变量
process.env.NODE_ENV = 'development';

module.exports = {
    entry: './src/index.js', 
    output: {
        filename: 'built.js',
        path: resolve(__dirname, 'build')
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    /*
                    css 兼容性处理： postcss --> postcss-loader postcss-preset-env
                    帮 postcss 找到 package.json 中 browserslist 里面的配置，通过配置加载指定的 css 兼容样式
                    // 2.
                        "browserslist": {
                            // 开发环境 --> 设置 node 环境变量: process.env.NODE_ENV = development
                            "development": [
                            "last 1 chrome version",
                            "last 1 firefox version",
                            "last 1 safari version"
                            ],
                            // 生成环境，默认是开启生产环境
                            "production": [
                            ">0.2%",
                            "not dead",
                            "not op_mini all"
                            ]
                        }
                    */
                    { // 1.
                        loader: 'postcss-loader',
                        options: {
                            idnet: 'postcss',
                            plugins: () => [
                                // postcss 插件
                                require('postcss-preset-env')()
                            ]
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        }),
        new MiniCssExtractPlugin({
            filename: 'css/built.css'
        })
    ],
    mode: 'development',

    devServer: {
        contentBase: resolve(__dirname, 'build'),
        compress: true,
        port: 3000,
        open: true
    }
}